//
//  UserInfoViewController.m
//  AppointmentForCar
//
//  Created by Herron on 16/10/4.
//  Copyright © 2016年 Herron. All rights reserved.
//

#import "UserInfoViewController.h"
#import "AppDelegate.h"
#import "ProvinceListViewController.h"
#import "CameraTakeMamanger.h"
#import "CityModel.h"

@interface UserInfoViewController ()

@property (weak, nonatomic) IBOutlet UILabel *phoneLabel;
@property (weak, nonatomic) IBOutlet UITextField *nicknameTextField;
@property (weak, nonatomic) IBOutlet UILabel *cityLabel;
@property (weak, nonatomic) IBOutlet UIImageView *avatarImageView;
@property (nonatomic, copy) NSString *avatarUrl;            // 头像
@property (nonatomic, copy) NSString *cityId;               // 城市id
@property (nonatomic, copy) NSString *gender;               // 性别
@property (weak, nonatomic) IBOutlet UIButton *maleBtn;
@property (weak, nonatomic) IBOutlet UIButton *femaleBtn;

@end

@implementation UserInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"完善资料";
    self.phoneLabel.text = self.phone;
    self.gender = @"男";
    
    NSNotificationCenter *notification = [NSNotificationCenter defaultCenter];
    [notification addObserver:self selector:@selector(selectCity:) name:@"sendCity" object:nil];
}

- (void)viewWillAppear:(BOOL)animated {
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.hidden = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - <CityListViewControllerDelegate>
- (void)selectCity:(NSNotification *)notification {
    NSDictionary *dic = notification.userInfo;
    CityModel *city = [[CityModel alloc] initWithDictionary:dic error:nil];
    self.cityLabel.text = city.name;
    self.cityId = city.cityId;
}
#pragma mark - event mothed
- (IBAction)submitClick:(id)sender {
    if (!self.avatarUrl.length) {
        [HUDManager showWarningWithText:@"请上传头像"];
        return;
    }
    if (!self.nicknameTextField.text.length) {
        [HUDManager showWarningWithText:@"请输入昵称"];
        return;
    }
    if (!self.cityId.length) {
        [HUDManager showWarningWithText:@"请选择服务地区"];
        return;
    }
    
    NSDictionary *parameDic = @{@"phoneNumber":self.phone,
                                @"avatarUrl":self.avatarUrl,
                                @"nickName":self.nicknameTextField.text,
                                @"cityId":self.cityId,
                                @"gender":self.gender,
                                @"invitationCode":self.invitationCode};
    NSLog(@"parameDic = %@", parameDic);
    [HUDManager showLoadingHUDViewWithText:@"正在注册"];
    [NetWork postNetWorkWithUrl:kURLUserRegister parame:parameDic successBlock:^(NSDictionary *dic) {
        NSLog(@"register = %@", dic);
        if ([dic[@"status"] isEqualToNumber:@(0)]) {
            [HUDManager hideHUDView];
            AppDelegate *appDelegate = kAppDelegate;
            [appDelegate setRootViewController];
        } else {
            [HUDManager showWarningWithText:dic[@"message"]];
        }
    } errorBlock:^(id error) {
        [HUDManager showWarningWithText:@"网络异常"];
    }];
}


// 地区
- (IBAction)cityClick:(id)sender {
    ProvinceListViewController *vc = [[ProvinceListViewController alloc] init];
    vc.type = @"1";
    [self.navigationController pushViewController:vc animated:YES];}

// 头像
- (IBAction)uploadAvatarClick:(id)sender {
    [[CameraTakeMamanger sharedInstance] cameraSheetInController:self handler:^(UIImage *image, NSString *imagePath) {
        NSLog(@"imagePath = %@", imagePath);
        [NetWork uploadImageWithUrl:kURLUploadImage image:image imageName:@"avatar" successBlock:^(NSDictionary *dic) {
            NSLog(@"dic = %@", dic);
            NSArray *urls = dic[@"data"][@"urls"];
            self.avatarUrl = [urls firstObject];
            [self.avatarImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", kRootImageUrl, self.avatarUrl]] placeholderImage:[UIImage imageNamed:@"registerAvatar"]];
        } errorBlock:^(id error) {
            [HUDManager showWarningWithText:@"上传失败"];
        }];
    }];
}

// 性别
- (IBAction)genderClick:(UIButton *)sender {
    if (sender.tag == 1) {
        self.gender = @"男";
        [sender setImage:[UIImage imageNamed:@"selected"] forState:UIControlStateNormal];
        [self.femaleBtn setImage:[UIImage imageNamed:@"noSelect"] forState:UIControlStateNormal];
    } else {
        self.gender = @"女";
        [sender setImage:[UIImage imageNamed:@"selected"] forState:UIControlStateNormal];
        [self.maleBtn setImage:[UIImage imageNamed:@"noSelect"] forState:UIControlStateNormal];
    }
}


@end
